package com.spring.boot.springBoot.redisEache.controller;

import com.spring.boot.springBoot.redisEache.entity.DemoInfo;
import com.spring.boot.springBoot.redisEache.service.DemoInfoService;
import com.spring.boot.springBoot.redisEache.utils.JedisTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by pactera on 2017/9/28.
 */
@Controller
public class DemoInfoController {

    @Autowired
    DemoInfoService demoInfoService;

    @Autowired
    private JedisTemplate jedisTemplate;


    @RequestMapping("/find")
    public @ResponseBody String test() throws Exception{
        DemoInfo loaded = demoInfoService.findById("1");
        System.out.println("loaded="+loaded);
        DemoInfo cached = demoInfoService.findById("1");
        System.out.println("cached="+cached);
        loaded = demoInfoService.findById("2");
        System.out.println("loaded2="+loaded);

        DemoInfo info = jedisTemplate.getBean("demoinfo",DemoInfo.class);
        System.out.print(info.toString());
        return "ok";
    }


    @RequestMapping("/delete")
    public @ResponseBody String delete(){
        demoInfoService.deleteFromCache("1");
        return"ok";
    }
    @GetMapping("/save")
    public @ResponseBody String save() throws Exception{
        DemoInfo demoInfo = new DemoInfo();
        demoInfo.setUserName("张三");
        demoInfo.setPassword("123456");
        demoInfoService.save(demoInfo);
        jedisTemplate.saveBean("demoinfo",demoInfo);


        return "OK";
    }

}